/**
 * 
 */
package org.mspring.crm.client.frame;

import java.util.List;

import org.mspring.crm.client.model.ItemModel;
import org.mspring.crm.client.remote.ItemRemote;

import com.extjs.gxt.ui.client.Style.HideMode;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.TabPanel.TabPosition;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author Gao Youbo
 * @since 2012-3-12
 * 
 */
public class SimpleFrameCreator extends AbstractFrameCreator {

    private TabPanel tabPanel;

    /* (non-Javadoc)
     * @see org.mspring.crm.client.widget.frame.AbstractFrameCreator#createFrame(org.mspring.crm.client.model.ItemModel[])
     */
    @Override
    public Widget createFrame(ItemModel... items) {
        // TODO Auto-generated method stub
        tabPanel = new TabPanel();
        tabPanel.setTabPosition(TabPosition.BOTTOM);
        tabPanel.setBorderStyle(false);
        tabPanel.setBodyBorder(false);

        createSimpleFrame(items[0]);
        return tabPanel;
    }

    private void createSimpleFrame(ItemModel panel) {
        ItemRemote.Util.getInstance().findTabItem(panel.getId(), new AsyncCallback<List<ItemModel>>() {
            @Override
            public void onSuccess(List<ItemModel> result) {
                // TODO Auto-generated method stub
                if (result != null && result.size() > 0) {
                    for (ItemModel itemModel : result) {
                        TabItem tabItem = new TabItem();
                        tabItem.setScrollMode(Scroll.AUTO);
                        // if (entry.isFill()) {
                        tabItem.setLayout(new FitLayout());
                        tabItem.setScrollMode(Scroll.NONE);
                        // }

                        // tabItem.setHideMode(entry.getHideMode());
                        tabItem.setHideMode(HideMode.DISPLAY);
                        tabItem.setText(itemModel.getName());
                        
                        tabPanel.add(tabItem);
                        if (itemModel.getSelected()) {
                            tabPanel.setSelection(tabItem);
                        }
                    }
                }
            }

            @Override
            public void onFailure(Throwable caught) {
                // TODO Auto-generated method stub

            }
        });
    }
}
